package utils

import (
	"io"
	"log/slog"
	"os"
	"strings"

	"buf.build/go/protoyaml"
	"google.golang.org/protobuf/proto"
	"gopkg.in/yaml.v2"
)

var (
	configPath = ""
)

func init() {
	configPath = os.Getenv("CONFIG_BASE_PATH")
	if configPath == "" {
		configPath = "./"
	}

}

func ListFile(fileFolder string) []string {
	if strings.HasSuffix(configPath, "/") && strings.HasPrefix(fileFolder, "/") {
		fileFolder = strings.TrimPrefix(fileFolder, "/")
	}

	if !strings.HasPrefix(fileFolder, "/") {
		fileFolder = configPath + fileFolder
	}

	dirs, err := os.ReadDir(fileFolder)
	if err != nil {
		return nil
	}
	var paths []string
	for _, dir := range dirs {
		t := dir.Type()
		if t == 0 {
			paths = append(paths, dir.Name())
		}
	}
	return paths
}

func ReadPBYamlConfig(filepath string, out proto.Message) error {
	if strings.HasSuffix(configPath, "/") && strings.HasPrefix(filepath, "/") {
		filepath = strings.TrimPrefix(filepath, "/")
	}

	filepath = configPath + filepath
	file, err := os.Open(filepath)
	if err != nil {
		slog.Error("Error opening YAML file.", slog.Any("error", err))
	}
	defer file.Close() // 确保在函数退出时关闭文件

	// 读取文件内容
	data, err := io.ReadAll(file)
	if err != nil {
		slog.Error("Error reading YAML file.", slog.Any("error", err))
	}

	// 解析YAML内容到结构体
	err = protoyaml.Unmarshal(data, out)
	if err != nil {
		slog.Error("Error unmarshaling YAML.", slog.Any("error", err))
	}
	return nil
}
func ReadYamlConfig(filepath string, out interface{}) error {
	if strings.HasSuffix(configPath, "/") && strings.HasPrefix(filepath, "/") {
		filepath = strings.TrimPrefix(filepath, "/")
	}

	filepath = configPath + filepath
	file, err := os.Open(filepath)
	if err != nil {
		slog.Error("Error opening YAML file.", slog.Any("error", err))
	}
	defer file.Close() // 确保在函数退出时关闭文件

	// 读取文件内容
	data, err := io.ReadAll(file)
	if err != nil {
		slog.Error("Error reading YAML file.", slog.Any("error", err))
	}

	// 解析YAML内容到结构体
	err = yaml.Unmarshal(data, out)
	if err != nil {
		slog.Error("Error unmarshaling YAML.", slog.Any("error", err))
	}
	return nil
}

func ReadFile(filepath string) ([]byte, error) {
	_, e := os.Stat(filepath)
	if e != nil {
		filepath = configPath + filepath
	}
	filepath = filepath
	file, err := os.Open(filepath)
	if err != nil {
		slog.Error("Error opening YAML file.", slog.Any("error", err))
	}
	defer file.Close() // 确保在函数退出时关闭文件

	// 读取文件内容
	data, err := io.ReadAll(file)
	if err != nil {
		slog.Error("Error reading YAML file.", slog.Any("error", err))
	}

	return data, nil
}

func WriteFile(filepath string, content string) error {
	file, err := os.OpenFile(filepath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
	if err != nil {
		slog.Error("Error opening YAML file.", slog.Any("error", err))
	}

	_, err = file.Write([]byte(content))
	return err
}

func ReadFileStr(filepath string) (string, error) {
	filepath = configPath + filepath
	file, err := os.Open(filepath)
	if err != nil {
		slog.Error("Error opening YAML file.", slog.Any("error", err))
	}
	defer file.Close() // 确保在函数退出时关闭文件

	// 读取文件内容
	data, err := io.ReadAll(file)
	if err != nil {
		slog.Error("Error reading YAML file.", slog.Any("error", err))
	}

	return string(data), nil
}

func WriteConfig(filepath string, data interface{}) error {
	if strings.HasSuffix(configPath, "/") && strings.HasPrefix(filepath, "/") {
		filepath = strings.TrimPrefix(filepath, "/")
	}

	filepath = configPath + filepath
	file, err := os.OpenFile(filepath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
	if err != nil {
		slog.Error("Error opening YAML file.", slog.Any("error", err))
	}
	defer file.Close() // 确保在函数退出时关闭文件

	out, err := yaml.Marshal(data)
	if err != nil {
		slog.Error("Error marshaling YAML.", slog.Any("error", err))
		return err
	}

	_, err = file.Write(out)
	if err != nil {
		slog.Error("Error writing YAML file.", slog.Any("error", err))
	}
	return nil
}
